package Reader;

import org.junit.jupiter.api.Test;

import java.io.*;

public class FileReaderTest {

    /*
    字符流--Reader，按照字符来读取文件
     */

    @Test
    public void readerTestBySingleChar() throws IOException {
        String filePath = "D:\\aaaa\\test3.txt";
        File file = new File(filePath);
        FileReader fileReader=null;
        if(file.exists()) {
            int data = 0;
            fileReader = new FileReader(file);
            //使用read()方法，每次读取一个字符。因为是字符，中英文皆可
            while((data = fileReader.read())!=-1){
                System.out.print((char) data);
            }
        }
        fileReader.close();


    }
    //输出：我是张瑞阳。My name is ZhangRuiyang

    @Test
    public void readerTestByMultiChar() throws IOException {
        String filePath = "D:\\aaaa\\test3.txt";
        File file = new File(filePath);
        FileReader fileReader=null;
        char[] buf = new char[8];
        int readLength = 0;
        if(file.exists()) {
            fileReader = new FileReader(file);
            /*使用read(char[] a)方法，每次读取字符填入数组a，并返回读取的字符数目
            读取完毕返回-1；
            因为是字符，中英文皆可*/
            while((readLength = fileReader.read(buf))!=-1){
                System.out.print(new String(buf,0,readLength));
            }
        }
        fileReader.close();

    }
    //输出：我是张瑞阳。My name is ZhangRuiyang
}
